package com.black.platform.position.service.handler.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.black.common.web.PageQuery;
import com.black.platform.position.po.Position;
import com.black.platform.position.service.biz.PositionService;
import com.black.platform.position.service.handler.PositionHandlerService;
import com.black.platform.position.vo.PositionRoleVo;
import com.black.platform.role.po.PositionRole;
import com.black.platform.role.service.bz.PositionRoleService;
import com.black.platform.role.vo.IdVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PositionHandlerServiceImpl implements PositionHandlerService {

    private final PositionService positionService;

    private final PositionRoleService positionRoleService;

    @Override
    public IPage<Position> search(PageQuery<Position> searchVo) {
        return positionService.search(searchVo);
    }

    @Override
    public List<Position> getAllPositions() {
        return positionService.list();
    }

    @Override
    public PositionRoleVo getPositionRoleVoByPositionId(Long positionId) {
        PositionRoleVo positionRoleVo = new PositionRoleVo();
        Position position = positionService.getById(positionId);
        positionRoleVo.setPosition(position);
        QueryWrapper<PositionRole> query = new QueryWrapper<>();
        query.eq("position_id", positionId);
        List<PositionRole> roleList = positionRoleService.list(query);
        positionRoleVo.setRoleIds(new ArrayList<>());
        roleList.forEach(role -> {
            IdVo idVo = new IdVo(role.getRoleId());
            positionRoleVo.getRoleIds().add(idVo);
        });
        return positionRoleVo;
    }

    @Override
    public boolean saveOrUpdate(PositionRoleVo positionRoleVo) {
        if (positionRoleVo.getPosition().getId() != null) {
            QueryWrapper<PositionRole> query = new QueryWrapper<>();
            query.eq("position_id", positionRoleVo.getPosition().getId());
            positionRoleService.remove(query);
        }
        positionService.saveOrUpdate(positionRoleVo.getPosition());
        if (positionRoleVo.getRoleIds() != null && positionRoleVo.getRoleIds().size() > 0) {
            List<PositionRole> positionRoleList = positionRoleVo.getRoleIds().stream().map(idVo -> {
                PositionRole positionRole = new PositionRole();
                positionRole.setPositionId(positionRoleVo.getPosition().getId());
                positionRole.setRoleId(idVo.getId());
                return positionRole;
            }).collect(Collectors.toList());
            positionRoleService.saveBatch(positionRoleList);
        }
        return true;
    }
}
